package com.andyliu.configer;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.util.Properties;

/**
 * ClassName: MybatisConfiger
 * Description: mybatis配置
 * date: 2021/1/19 5:10 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@Configuration
@MapperScan({"com.andyliu.sys.mapper"})
@PropertySource("classpath:jdbc.properties")
public class MybatisConfiger {
    @Value("${jdbc.driver}")
    private String driver="com.mysql.cj.jdbc.Driver";
    @Value("${jdbc.url}")
    private String url="jdbc:mysql://localhost/manager";
    @Value("${jdbc.username}")
    private String userName="root";
    @Value("${jdbc.pwd}")
    private String pwd="Lh_198571";

    @Bean
    public PropertySourcesPlaceholderConfigurer getPropertySources(){
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DruidDataSource getDruidDataSource() throws IOException {
        Properties pro = new Properties();
        pro = Resources.getResourceAsProperties("jdbc.properties");
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(pro.getProperty("jdbc.driver"));
        ds.setUrl(pro.getProperty("jdbc.url"));
        ds.setUsername(pro.getProperty("jdbc.username"));
        ds.setPassword(pro.getProperty("jdbc.pwd"));
        ds.setMaxActive(Integer.parseInt(pro.getProperty("jdbc.maxActive")));
        return ds;
    }
    /**配置PageInterceptor插件*/
    @Bean
    public PageInterceptor getPageInterceptor() {
        PageInterceptor pageIntercptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("reasonable", "true");
        properties.setProperty("pageSizeZero", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageIntercptor.setProperties(properties);
        return pageIntercptor;
    }
        @Bean
    public SqlSessionFactoryBean getFactoryBean() throws IOException {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(this.getDruidDataSource());
        factoryBean.setTypeAliasesPackage("com.andyliu");
        factoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
        PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_URL_PREFIX+"/com/**/*.xml";//数据库层xml文件地址
        factoryBean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));
        factoryBean.setPlugins(new Interceptor[] {this.getPageInterceptor()});
        return factoryBean;
    }

}
